package cn.china.xfood.controller.car;


import cn.china.xfood.service.UsermenuService;
import cn.china.xfood.service.UserorderService;
import entity.Product;
import entity.Usermenu;
import entity.Userorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class car {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    UserorderService userorderService;

    @Autowired
    UsermenuService usermenuService;

    @RequestMapping("/getCar")
    public List<Object> getCar(@RequestParam String username, HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin", "*");
        List<Object> range = redisTemplate.opsForList().range(username, 0, redisTemplate.opsForList().size(username) - 1);
//        List<Product> list = new ArrayList<>();//集合必须要有实现类 ,List<Product> list = null;list.add方法失效
//
//        for (int i = 0; i < range.size(); i++) {
//
//            Product product = (Product)range.get(i);
//            list.add(product);
//        }

        return range;
    }


    @RequestMapping("/userOrder")
    public String userOrder(@RequestParam String username,HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        List<Object> range = redisTemplate.opsForList().range(username, 0, redisTemplate.opsForList().size(username) - 1);
        Userorder userorder = new Userorder();
        List<Usermenu> usermenuList = new ArrayList<>();
        double totolPrice = 0;

        long orderTime = System.currentTimeMillis();
        String currentTime = String.valueOf(orderTime);

        for (int i = 0; i < range.size(); i++) {
            Product product = (Product) range.get(i);

            // 计算得到商品总价
            double price = product.getProductPrice().doubleValue();
            Integer productAmount = product.getProductAmount();
            totolPrice = totolPrice +price*productAmount;


            Usermenu usermenu = new Usermenu(currentTime,product.getId(),product.getProductAmount(),product.getProductPrice());
            usermenuList.add(usermenu);

        }


    /*    Integer orderNumber = (Integer) redisTemplate.opsForValue().get("orderNumber");
        if(orderNumber==null){
            redisTemplate.opsForValue().set("orderNumber",1);
            userorder.setOrderNumber("1");
        }else {
            orderNumber++;
            redisTemplate.opsForValue().set("orderNumber",orderNumber);
            userorder.setOrderNumber(orderNumber.toString());
        }

        System.out.println();
        */

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String orderTimeNow = sdf.format(date);

        /*封装 userorder 对象*/
        userorder.setOrderNumber(currentTime);
        userorder.setUsername(username);
        userorder.setOrderTime(orderTimeNow);
        userorder.setTotalPrice(totolPrice);
        userorder.setPayStatus("1");
        // 插入数据到    订单表 和 用户菜品信息表
        int i = userorderService.insertSelective(userorder);

        if (i>0){
            int insertlist = usermenuService.insertlist(usermenuList);
            if (insertlist>0){
                redisTemplate.opsForList().getOperations().delete(username);
                return "添加订单成功";
            }else{
                return "添加失败";
            }
        }else{
            return "添加失败";
        }
    }


    /*

    usermenu表和userorder表都有同一个order_number字段，如果两个表此字段添加的值不一样会报错：
    Cannot add or update a child row: a foreign key constraint fails

     */


    //生成订单编号
    public String orderNum(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String currentTime = sdf.format(date);
        //例：    2021-03-29 11:14:11
        String[] s = currentTime.split(" ");

        //2021-03-29
        String[] split = s[0].split("-");String suffix ="";

        for (int j = 0; j < split.length; j++) {
            suffix = suffix + split[j];
        }

        //11:14:11
        String[] split1 = s[1].split(":");String close = "";
        for (int i = 0; i < split1.length; i++) {
            close = close + split1[i];
        }

        System.out.println("订单号："+suffix +close);
        return (suffix+close);



    }


    @RequestMapping("/cleanCar")
    public String cleanCar(@RequestParam String username, HttpServletResponse res){
        res.setHeader("Access-Control-Allow-Origin","*");
        redisTemplate.opsForList().getOperations().delete(username);
        List<Object> range = redisTemplate.opsForList().range(username, 0, redisTemplate.opsForList().size(username) - 1);
        if (range.isEmpty()){
            return "清空成功";
        }else{
            return "清空失败";
        }
    }

}
